<?php

use App\Models\Career;
use App\Models\Course;
use App\Models\Skill;
use Illuminate\Database\Seeder;

class SkillCourseTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $skills = Skill::all();
        $courses = Course::all()->toArray();
        $courses_count = count($courses);
        foreach ($skills as $skill) {
            $attaches = [];
            $count = rand(3, $courses_count);
            $rand = array_rand($courses, $count);
            foreach ($rand as $key => $courseId) {
                $next_key = $key < $count - 1 ? $rand[$key + 1] : null;
                $attaches[$courseId] = ['next_course_id' => [$next_key], 'step' => $key];
            }
            $skill->courses()->attach($attaches);
        }
    }
}
